#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  File-Name:	local-news-analysis.R
#  Date:	July 9, 2021
#  Author: Joao Gonzales / Bernhard Clemm von Hohenberg
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# SETUP  ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

rm(list=ls())

library(tidyverse)
library(lmerTest)
library(dotwhisker)
library(broom)
library(ggpubr)
library(plm)
library(stargazer)
library(bife)

data_path <- "/Users/bernhardclemm/Dropbox/Mac/Documents/Academia/EXPO/repositories/EXPO2.0/Projects/Local-News/Data/"
output_path <- "/Users/bernhardclemm/Dropbox/Mac/Documents/Academia/EXPO/repositories/EXPO2.0/Projects/Local-News/Output/"
code_path <- "/Users/bernhardclemm/Dropbox/Mac/Documents/Academia/EXPO/repositories/EXPO2.0/Projects/Local-News/Code/"

source(paste0(code_path, "local-news-recoding.R"))

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# CORRELATIONAL MODELS (W1 ONLY) ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

models_lm <- list()

# Effect of local news ####

models_lm$know_loc_lm <- lm(
  polknow_w3 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w3 + party_w3 + loc_w3_mean_log + visits_u_w3_log, data = final_data_w3)
models_lm$particip_loc_lm <- lm(
  participation_w2_bin ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w2 + party_w2 + loc_w2_mean_log + visits_u_w2_log, data = final_data_w2)
models_lm$ap_loc_lm <- lm(
  ap_w1 ~ edu_high_w0_fac +white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + loc_w1_mean_log + visits_u_w1_log, data = final_data_w1)
models_lm$attextr_loc_lm <- lm(
  attextr_w1 ~ edu_high_w0_fac +white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + loc_w1_mean_log + visits_u_w1_log, data = final_data_w1)

# Effect of local news controlling for national news ####

models_lm$know_nat_lm <- lm(
  polknow_w3 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w3 + party_w3 + loc_w3_mean_log + nat_w3_mean_log + visits_u_w3_log, data = final_data_w3)
models_lm$particip_nat_lm <- lm(
  participation_w2_bin ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w2 + party_w2 + loc_w2_mean_log + nat_w2_mean_log + visits_u_w2_log, data = final_data_w2)
models_lm$ap_nat_lm <- lm(
  ap_w1 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + loc_w1_mean_log + nat_w1_mean_log + visits_u_w1_log, data = final_data_w1)
models_lm$attextr_nat_lm <- lm(
  attextr_w1 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + loc_w1_mean_log + nat_w1_mean_log + visits_u_w1_log, data = final_data_w1)

# Effect of local*national news ####

models_lm$know_locnat_lm <- lm(
  polknow_w3 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w3 + party_w3 + loc_w3_mean_log*nat_w3_mean_log  + visits_u_w3_log, 
  data = final_data_w3)
models_lm$particip_locnat_lm <- lm(
  participation_w2_bin ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w2 + party_w2 + loc_w2_mean_log*nat_w2_mean_log + visits_u_w2_log, 
  data = final_data_w2)
models_lm$ap_locnat_lm <- lm(
  ap_w1 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + loc_w1_mean_log*nat_w1_mean_log + visits_u_w1_log, 
  data = final_data_w1)
models_lm$attextr_locnat_lm <- lm(
  attextr_w1 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + loc_w1_mean_log*nat_w1_mean_log + visits_u_w1_log, 
  data = final_data_w1)

# Effect of local*education ####

models_lm$know_edu_lm <- lm(
  polknow_w3 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w3 + party_w3 + nat_w3_mean_log + loc_w3_mean_log*edu_high_w0_fac  + visits_u_w3_log, 
  data = final_data_w3)
models_lm$particip_edu_lm <- lm(
  participation_w2_bin ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w2 + party_w2 + nat_w2_mean_log + loc_w2_mean_log*edu_high_w0_fac + visits_u_w2_log, 
  data = final_data_w2)
models_lm$ap_edu_lm <- lm(
  ap_w1 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + nat_w1_mean_log + loc_w1_mean_log*edu_high_w0_fac + visits_u_w1_log, 
  data = final_data_w1)
models_lm$attextr_edu_lm <- lm(
  attextr_w1 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + nat_w1_mean_log + loc_w1_mean_log*edu_high_w0_fac + visits_u_w1_log, 
  data = final_data_w1)

# Effect of local*white ####

models_lm$know_white_lm <- lm(
  polknow_w3 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w3 + party_w3 + nat_w3_mean_log + loc_w3_mean_log*white_w0_fac  + visits_u_w3_log, 
  data = final_data_w3)
models_lm$particip_white_lm <- lm(
  participation_w2_bin ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w2 + party_w2 + nat_w2_mean_log + loc_w2_mean_log*white_w0_fac + visits_u_w2_log, 
  data = final_data_w2)
models_lm$ap_white_lm <- lm(
  ap_w1 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + nat_w1_mean_log + loc_w1_mean_log*white_w0_fac + visits_u_w1_log, 
  data = final_data_w1)
models_lm$attextr_white_lm <- lm(
  attextr_w1 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + nat_w1_mean_log + loc_w1_mean_log*white_w0_fac + visits_u_w1_log, 
  data = final_data_w1)

# Effect of local*interest ####

models_lm$know_int_lm <- lm(
  polknow_w3 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w3 + party_w3 + nat_w3_mean_log + loc_w3_mean_log*int_politics_w2w3  + visits_u_w3_log, 
  data = final_data_w3)
models_lm$particip_int_lm <- lm(
  participation_w2_bin ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w2 + party_w2 + nat_w2_mean_log + loc_w2_mean_log*int_politics_w2w3 + visits_u_w2_log, 
  data = final_data_w2)
models_lm$ap_int_lm <- lm(
  ap_w1 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + nat_w1_mean_log + loc_w1_mean_log*int_politics_w2w3+ visits_u_w1_log, 
  data = final_data_w1)
models_lm$attextr_int_lm <- lm(
  attextr_w1 ~ edu_high_w0_fac + white_w0_fac + age_w0 + gender_w0_fac +
    ideo_w1 + party_w1 + nat_w1_mean_log + loc_w1_mean_log*int_politics_w2w3+ visits_u_w1_log, 
  data = final_data_w1)

# Tables ####

## Rename coefficients

rename_coeffs <- function(reg) {
  names(reg[["coefficients"]]) <- gsub(
    "ideo_w1|ideo_w2|ideo_w3", "ideo", names(reg[["coefficients"]]))
  names(reg[["coefficients"]]) <- gsub(
    "party_w1|party_w2|party_w3", "party", names(reg[["coefficients"]]))
  names(reg[["coefficients"]]) <- gsub(
    "loc_w1_mean_log|loc_w2_mean_log|loc_w3_mean_log", "loc_mean_log", names(reg[["coefficients"]]))
  names(reg[["coefficients"]]) <- gsub(
    "nat_w1_mean_log|nat_w2_mean_log|nat_w3_mean_log", "nat_mean_log", names(reg[["coefficients"]]))
  names(reg[["coefficients"]]) <- gsub(
    "visits_u_w1_log|visits_u_w2_log|visits_u_w3_log", "visits_log", names(reg[["coefficients"]]))
  return(reg)
}

models_lm <- lapply(models_lm, rename_coeffs)

## Table 2 (main paper)

stargazer(models_lm$know_loc_lm, models_lm$know_nat_lm, 
          models_lm$particip_loc_lm, models_lm$particip_nat_lm, 
          models_lm$ap_loc_lm, models_lm$ap_nat_lm, 
          models_lm$attextr_loc_lm, models_lm$attextr_nat_lm, 
          type = "html", out = paste0(output_path, "/tables/lm_onewave_main.html"),
          omit.stat = c("f", "ser"), star.cutoffs = c(0.05, 0.01, 0.001),
          covariate.labels = c(
            "Local news exposure", "National news exposure",
            "Education: High", "Ethnicity: White", "Age", "Gender: Male",
            "Ideology", "Partisanship"),
          order = c(7, 8, 1:6),
          dep.var.caption = "Main effect models",
          dep.var.labels = c("Knowledge", "Participation",
                             "Affective polarization", "Attitude extremity")) 

## Table A5.1 (Appendix)

stargazer(models_lm$know_locnat_lm, models_lm$know_edu_lm, 
          models_lm$know_white_lm, models_lm$know_int_lm, 
          models_lm$particip_locnat_lm, models_lm$particip_edu_lm, 
          models_lm$particip_white_lm, models_lm$particip_int_lm, 
          type = "html", out = paste0(output_path, "/tables/lm_onewave_interactions-1.html"),
          omit.stat = c("f", "ser", "adj.rsq"),
          covariate.labels = c(
            "Local news exposure", "National news exposure", "Overall browsing frequency",
            "Local * National", "Local * education", "Local * White", "Local * interest",
            "Education: High", "Ethnicity: White", "Age", "Gender: Male",
            "Ideology", "Partisanship", "Interest"),
          order = c(7, 9:10, 11:14, 1:6),
          dep.var.caption = "Interaction models",
          dep.var.labels  = c("Knowledge", "Participation"))

## Table A5.2 (Appendix)

stargazer(models_lm$ap_locnat_lm, models_lm$ap_edu_lm, 
          models_lm$ap_white_lm, models_lm$ap_int_lm, 
          models_lm$attextr_locnat_lm, models_lm$attextr_edu_lm, 
          models_lm$attextr_white_lm, models_lm$attextr_int_lm, 
          type = "html", out = paste0(output_path, "/tables/lm_onewave_interactions-2.html"),
          omit.stat = c("f", "ser", "adj.rsq"),
          covariate.labels = c(
            "Local news exposure", "National news exposure", "Overall browsing frequency",
            "Local * National", "Local * education", "Local * White", "Local * interest",
            "Education: High", "Ethnicity: White", "Age", "Gender: Male",
            "Ideology", "Partisanship", "Interest"),
          order = c(7, 9:10, 11:14, 1:6),
          dep.var.caption = "Interaction models",
          dep.var.labels  = c("Affective polarization", "Attitude extremity"))

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# WITHIN-PERSON MODELS (FIXED-EFFECTS) ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

models_fe <- list()

# Effect of local news ####

models_fe$particip_loc <- plm(
  participation ~ loc_mean_log + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$ap_loc <- plm(
  ap ~ loc_mean_log + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$attextr_loc <- plm(
  attextr ~ loc_mean_log + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Effect of local, controlling for national news ####

models_fe$particip_nat <- plm(
  participation ~ loc_mean_log + nat_mean_log + visits_u_log, 
    data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$ap_nat <- plm(
  ap ~ loc_mean_log + nat_mean_log + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$attextr_nat <- plm(
  attextr ~ loc_mean_log + nat_mean_log + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Effect of local*national news ####

models_fe$particip_locnat <- plm(
  participation ~ loc_mean_log*nat_w1w2w3_mean_log + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$ap_locnat <- plm(
  ap ~ loc_mean_log*nat_w1w2w3_mean_log + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$attextr_locnat <- plm(
  attextr ~ loc_mean_log*nat_w1w2w3_mean_log + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Effect of local*education ####

models_fe$particip_edu <- plm(
  participation ~ loc_mean_log*edu_high_w0_fac + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$ap_edu <- plm(
  ap ~ loc_mean_log*edu_high_w0_fac + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$attextr_edu <- plm(
  attextr ~ loc_mean_log*edu_high_w0_fac + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Effect of local*white ####

models_fe$particip_white <- plm(
  participation ~ loc_mean_log*white_w0_fac + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$ap_white <- plm(
  ap ~ loc_mean_log*white_w0_fac + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$attextr_white <- plm(
  attextr ~ loc_mean_log*white_w0_fac + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Effect of local*interest ####

models_fe$particip_int <- plm(
  participation ~ loc_mean_log*int_politics_w2w3 + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$ap_int <- plm(
  ap ~ loc_mean_log*int_politics_w2w3 + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe$attextr_int <- plm(
  attextr ~ loc_mean_log*int_politics_w2w3 + visits_u_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Tables ####

## Table 3 (main paper)

stargazer(models_fe$particip_nat, models_fe$particip_loc, 
          models_fe$ap_nat, models_fe$ap_loc, 
          models_fe$attextr_nat, models_fe$attextr_loc,
          type = "html", out = paste0(output_path, "/tables/fe_main.html"),
          omit.stat = c("f", "ser"), star.cutoffs = c(0.05, 0.01, 0.001),
          covariate.labels = c(
            "Local news exposure", "National news exposure", 
            "Overall browsing frequency"),
          # order = c(6, 7, 1:5),
          dep.var.caption = "Main effect models",
          dep.var.labels = c("Participation",
                             "Affective polarization", "Attitude extremity")) 

## Table 4 (main paper)

stargazer(models_fe$particip_locnat, models_fe$particip_edu, 
          models_fe$particip_white, models_fe$particip_int, 
          models_fe$ap_locnat, models_fe$ap_edu, 
          models_fe$ap_white, models_fe$ap_int, 
          models_fe$attextr_locnat, models_fe$attextr_edu, 
          models_fe$attextr_white, models_fe$attextr_int, 
          type = "html", out = paste0(output_path, "/tables/fe_interactions.html"),
          omit.stat = c("f", "ser"),
          covariate.labels = c(
          "Local news exposure", "Overall visits frequency",
          "Local * National", "Local * education", "Local * White", "Local * interest"),
          order = c(7:11, 13, 1:6, 14),
          dep.var.caption = "Interaction models",
          dep.var.labels  = c("Participation",
                              "Affective polarization", "Attitude extremity"))

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ROBUSTNESS: PROPORTIONAL MEASURE ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

models_fe_rob1 <- list()

# Effect of local news ####

models_fe_rob1$particip_loc <- plm(
  participation ~ loc_perc_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$ap_loc <- plm(
  ap ~ loc_perc_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$attextr_loc <- plm(
  attextr ~ loc_perc_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Effect of local, controlling for national news ####

models_fe_rob1$particip_nat <- plm(
  participation ~ loc_perc_log + nat_perc_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$ap_nat <- plm(
  ap ~ loc_perc_log + nat_perc_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$attextr_nat <- plm(
  attextr ~ loc_perc_log + nat_perc_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Effect of local*national news ####

models_fe_rob1$particip_locnat <- plm(
  participation ~ loc_perc_log*nat_w1w2w3_perc_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$ap_locnat <- plm(
  ap ~ loc_perc_log*nat_w1w2w3_perc_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$attextr_locnat <- plm(
  attextr ~ loc_perc_log*nat_w1w2w3_perc_log, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Effect of local*education ####

models_fe_rob1$particip_edu <- plm(
  participation ~ loc_perc_log*edu_high_w0_fac, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$ap_edu <- plm(
  ap ~ loc_perc_log*edu_high_w0_fac, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$attextr_edu <- plm(
  attextr ~ loc_perc_log*edu_high_w0_fac, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Effect of local*white ####

models_fe_rob1$particip_white <- plm(
  participation ~ loc_perc_log*white_w0_fac, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$ap_white <- plm(
  ap ~ loc_perc_log*white_w0_fac, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$attextr_white <- plm(
  attextr ~ loc_perc_log*white_w0_fac, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Effect of local*interest ####

models_fe_rob1$particip_int <- plm(
  participation ~ loc_perc_log*int_politics_w2w3, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$ap_int <- plm(
  ap ~ loc_perc_log*int_politics_w2w3, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")
models_fe_rob1$attextr_int <- plm(
  attextr ~ loc_perc_log*int_politics_w2w3, 
  data = final_data_allwaves_l, index = c("person_id"), model = "within")

# Tables ####

## Table A6.1 (Appendix)

stargazer(models_fe_rob1$particip_loc, models_fe_rob1$particip_nat, 
          models_fe_rob1$ap_loc, models_fe_rob1$ap_nat, 
          models_fe_rob1$attextr_loc, models_fe_rob1$attextr_nat, 
          type = "html", out = paste0(output_path, "/tables/fe_main_rob1.html"),
          omit.stat = c("f", "ser"), star.cutoffs = c(0.05, 0.01, 0.001),
          covariate.labels = c(
            "National news exposure", "Local news exposure"),
          dep.var.caption = "Main effect models",
          dep.var.labels = c("Participation",
                             "Affective polarization", "Attitude extremity")) 

## Table A6.2 (Appendix)

stargazer(models_fe_rob1$particip_locnat, models_fe_rob1$particip_edu, 
          models_fe_rob1$particip_white, models_fe_rob1$particip_int, 
          models_fe_rob1$ap_locnat, models_fe_rob1$ap_edu, 
          models_fe_rob1$ap_white, models_fe_rob1$ap_int, 
          models_fe_rob1$attextr_locnat, models_fe_rob1$attextr_edu, 
          models_fe_rob1$attextr_white, models_fe_rob1$attextr_int, 
          type = "html", out = paste0(output_path, "/tables/fe_interactions_rob1.html"),
          omit.stat = c("f", "ser"),
          covariate.labels = c(
            "Local news exposure", 
            "Local * National", "Local * education", "Local * White", "Local * interest"),
          dep.var.caption = "Interaction models",
          dep.var.labels = c("Participation",
                             "Affective polarization", "Attitude extremity")) 
